package com.huawei123.hasee.mynavigationview_view.TableLayoutAndViewPage;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.huawei123.hasee.mynavigationview_view.R;

import java.io.Serializable;

/**
 * 项目名称：MyNavigationView_view
 * 类描述：TabLayout+ViewPager实现导航栏切换Farment
 * 创建人：hasee
 * 创建时间：2017/6/23 19:35
 * 修改人：hasee
 * 修改时间：2017/6/23 19:35
 * 修改备注：网上 http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0731/3247.html 讲解很详细
 */
public class TabLayoutAndViewPagerActivity extends AppCompatActivity {
    public TabLayout mTL_title;
    public ViewPager mVP_content;


    private String[] title = {
            "系统",
            "发单",
            "接单"
    };


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tablayout_and_viewpager);
        mVP_content = (ViewPager) this.findViewById(R.id.mVP_content);
        mTL_title = (TabLayout) this.findViewById(R.id.mTL_title);

        MyPagerAdapter myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());

        //        for (int i = 0; i < myPagerAdapter.getCount(); i++) {
        //            TabLayout.Tab tab = mTL_title.getTabAt(i);//获得每一个tab
        //            tab.setCustomView(R.layout.tab_item);//给每一个tab设置view
        //            if (i == 0) {
        //                // 设置第一个tab的TextView是被选择的样式
        //                tab.getCustomView().findViewById(R.id.tab_text).setSelected(true);//第一个tab被选中
        //            }
        //            TextView textView = (TextView) tab.getCustomView().findViewById(R.id.tab_text);
        //            textView.setText(titles[i]);//设置tab上的文字
        //        }

        //第一种方法：1.TabLayout和Viewpager关联
        //        mTL_title.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        //            //选中回调
        //            @Override
        //            public void onTabSelected(TabLayout.Tab tab) {
        //                mVP_content.setCurrentItem(tab.getPosition(), true);
        //            }
        //
        //            //未选中回调
        //            @Override
        //            public void onTabUnselected(TabLayout.Tab tab) {
        //
        //            }
        //
        //            //重新选择回调
        //            @Override
        //            public void onTabReselected(TabLayout.Tab tab) {
        //
        //            }
        //        });
        //
        //        //2.ViewPager滑动关联tabLayout
        //        mVP_content.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTL_title));
        //
        //        //3.设置tabLayout的标签来自于PagerAdapter
        //        mTL_title.setTabsFromPagerAdapter(myPagerAdapter);
        //第二种方法；谷歌提供一步到位
        mTL_title.setupWithViewPager(mVP_content);
        for (int i = 0; i < mTL_title.getTabCount(); i++) {
            TabLayout.Tab tabAt = mTL_title.getTabAt(i);
            View view = View.inflate(this, R.layout.tablayout_item, null);
            TextView mTV_title = (TextView) view.findViewById(R.id.mTV_title);
            mTV_title.setText(title[i]);
            TextView mTV_pop = (TextView) view.findViewById(R.id.mTV_pop);
            mTV_pop.setText(0);
            tabAt.setCustomView(view);
        }
        mVP_content.setAdapter(myPagerAdapter);

    }

    /**
     * ViewPager适配器
     */
    class MyPagerAdapter extends FragmentPagerAdapter {


        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            //图片加标题的初始化展示
            //                        Drawable image = TabLayoutAndViewPagerActivity.this.getResources().getDrawable
            //             ( R.drawable.pop_image);
            //                        image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
            //                        SpannableString sb = new SpannableString("   " + title[position]);
            //                        ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
            //                        sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            String sb = title[position];//显示默认名
            return sb;
        }

        @Override
        public Fragment getItem(int position) {
            //给个个Fragment传的参数判定点击的是哪个tabLayout选项
            Fragment newsDetailFragment = new NewsDetailFragment();
            Bundle bundle = new Bundle();
            bundle.putString("title", title[position]);
            bundle.putInt("position", position);
            TabLayout.Tab tabAt = mTL_title.getTabAt(position);//为了能根据Fragment中展示的数据动态改变Tablayout消息提示小红点，把Tab传过去（笨成狗的方法）
            ActivityParameter activityParameter = new ActivityParameter();//TabLayout.Tab对象无法直接传过去，写个封装类
            activityParameter.setTabAt(tabAt);
            bundle.putSerializable("tab", activityParameter);
            newsDetailFragment.setArguments(bundle);
            return newsDetailFragment;
        }

        @Override
        public int getCount() {
            return title.length;
        }
    }


    class ActivityParameter implements Serializable {
        TabLayout.Tab tabAt;

        public TabLayout.Tab getTabAt() {
            return tabAt;
        }

        public void setTabAt(TabLayout.Tab tabAt) {
            this.tabAt = tabAt;
        }
    }
}
